package com.tian.view.anim.attr_anim

import android.animation.Animator
import android.animation.Animator.AnimatorListener
import android.animation.FloatEvaluator
import android.animation.ValueAnimator
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.view.animation.BounceInterpolator
import androidx.appcompat.app.AppCompatActivity
import androidx.fragment.app.Fragment
import com.tian.view.databinding.ViewFourBottonBinding

/**
 * @author tiansongtao, Email:tiansongtao@up366.com
 * @date 2022/12/14 17:57
 *
 * 其他常用函数
 * ValueAnimator.ofInt(), ValueAnimator.ofFloat()
 * setRepeatCount()   setRepeatMode()
 * cancel()
 * removeListener
 *
 *  * ValueAnimator 流程：
 *  ValueAnimator.ofInt(定义动画区间) --> Interpolator(返回当前进度eg:0.2) ---
 *  --> Evaluator(根据进度生成当前值)  ---> AnimatorUpdateListener(监听获取当前值).
 *
 */
class ValueAnimatorFragment : Fragment() {

    private lateinit var b: ViewFourBottonBinding


    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View {
        b = ViewFourBottonBinding.inflate(inflater)
        return b.root
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        (requireActivity() as AppCompatActivity).supportActionBar?.title = "ValueAnimator"
        b.mbtOne.text = "ValueAnimator"
        b.mbtOne.setOnClickListener {
            val anim = ValueAnimator.ofFloat(0f, 600f)
            anim.duration = 2000L
            //延时多久才开始动画
            anim.startDelay = 500L
            anim.interpolator = BounceInterpolator()
            anim.addUpdateListener {
                val value = it.animatedValue as Float
                b.mbtOne.translationY = value
            }
            anim.repeatCount = 4
            anim.repeatMode = ValueAnimator.REVERSE
            anim.setEvaluator(FloatEvaluator())
            anim.addListener(object : AnimatorListener {
                override fun onAnimationStart(animation: Animator?) {

                }

                override fun onAnimationEnd(animation: Animator?) {
                }

                override fun onAnimationCancel(animation: Animator?) {
                }

                override fun onAnimationRepeat(animation: Animator?) {
                }

            })
            anim.start()
        }

        b.mbtTwo.visibility = View.GONE
        b.mbtThree.visibility = View.GONE
        b.mbtFour.visibility = View.GONE
    }

}